package com.app.view.component.dialogs.feedBackView 
{
	import flash.display.MovieClip;
	import flash.display.Bitmap;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	import org.component.RadioButton;
	import org.component.Slider;
	import org.component.TabBar;
	
	import com.peachcastle.tools.Utils;
	
	import com.peachcastle.core.Display;
	import com.app.view.component.dialogs.FeedBackView;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class SendBug extends Display
	{
		private var _subject:FeedBackView = null;
		
		private var _screenShot:MovieClip = null;	// 截图容器
		private var _upLoad:RadioButton = null;     // 上传截图按钮
		
		private var _titleTips:TextField = null;    // 标题
		private var _inputText:TextField = null;    // 内容输入框
		private var _slider:Slider = null;          // 滚动条
		private var _warnTips:TextField = null;
		
		private var _tabBar:TabBar = null;
		private var _connection:Array = ["是錯誤", "是建議"]; 
		
		private const DEFAULT_TITLE_TEXT:String = "請輸入反饋標題...";		// 标题默认内容
		private const DEFAULE_INPUT_TEXT:String = "請輸入反饋內容...";		// 默认内容
		
		public function SendBug(ui:MovieClip) 
		{
			super(ui);
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			
			_tabBar = new TabBar();
			for (var i:int = 0; i < 2; i++ )
			{
				_tabBar.addItem(assest["radio" + i], _connection[i]);
			}
			_tabBar.selectIndex = 0;
			
			_upLoad = new RadioButton(assest["upLoad"]);
			_upLoad.setLabel("上傳截圖");
			
			_titleTips = assest["titleTips"];
			_inputText = assest["inputText"];
			_slider = new Slider(assest["sliderView"]);
			_warnTips = assest["warnTips"];
			
			_screenShot = assest["screenShot"];
			
			_titleTips.maxChars = 15;
			_inputText.maxChars = 200;
		}
		
		override protected function configEventListeners():void 
		{
			super.configEventListeners();	

			_titleTips.addEventListener(MouseEvent.CLICK, onTitleClick);
			_titleTips.addEventListener(Event.CHANGE, onTitleChange);
			_inputText.addEventListener(MouseEvent.CLICK, onInputClick);
			_inputText.addEventListener(Event.CHANGE, onInputChange); 
			_slider.addEventListener(Event.CHANGE, onChange);
		}
		
		private function onTitleClick(e:MouseEvent):void
		{
			if (Utils.trim(_titleTips.text) == DEFAULT_TITLE_TEXT)  _titleTips.htmlText = "";
			checkEmpty();
		}
		
		private function onTitleChange(e:Event):void
		{
			checkEmpty();
		}
		
		private function onInputClick(e:MouseEvent):void 
		{
			if (Utils.trim(_inputText.text) == DEFAULE_INPUT_TEXT) _inputText.htmlText = "";
			checkEmpty();
		}
		
		private function onInputChange(e:Event):void 
		{
			checkEmpty();
			_inputText.scrollV = _inputText.maxScrollV;
			_slider.update(5, _inputText.numLines);
			_slider.value = 100;
		}
		
		/**
		 * 滑动条改变事件处理函数
		 * @param	e
		 */
		private function onChange(e:Event):void 
		{
			var value:Number = _slider.value;
			_inputText.scrollV = Math.round(value / 100 * _inputText.maxScrollV);
		}
		
		private function checkEmpty():void
		{
			if (_inputText.text != "" && _titleTips.text != "")
			{
				if (_inputText.text != DEFAULE_INPUT_TEXT && _titleTips.text != DEFAULT_TITLE_TEXT)
				{
					if (_inputText.text.length < 8)
					{
						_warnTips.visible = true;
						_warnTips.text = "*反饋內容請在 8-200 之間";
						_subject.btn_ok.setDisable(true);
					}
					else if(_inputText.text.length >= 8 && _inputText.text.length < 200)
					{
						_warnTips.visible = false;
						_subject.btn_ok.setDisable(false);
					}	
					else if (_inputText.text.length >= 200)
					{
						_warnTips.visible = true;
						_warnTips.text = "*反饋內容請在 8-200 之間";
						_subject.btn_ok.setDisable(true);
					}
					
				} else
				{
					_subject.btn_ok.setDisable(true);
				}
				
			}
			else
			{
				_warnTips.visible = true;
				_subject.btn_ok.setDisable(true);
				
				if (_titleTips.text == "" || _titleTips.text == DEFAULT_TITLE_TEXT)
				{
					_warnTips.text = "請輸入反饋標題";	
				} else if (_inputText.text == "" || _inputText.text == DEFAULE_INPUT_TEXT)
				{
					_warnTips.text = "請輸入反饋內容";
				} else
				{
					_warnTips.text = "";
				}
			}	
		}
		
		public function initText():void
		{
			_titleTips.text == DEFAULT_TITLE_TEXT;
			_inputText.text == DEFAULE_INPUT_TEXT;
		}
		
		public function setScreenShot(bmp:Bitmap):void
		{
			if (_screenShot.numChildren>1)
			{
				_screenShot.removeChildAt(1);
			}
		
			bmp.width = _screenShot.width;
			bmp.height = _screenShot.height;
			_screenShot.addChild(bmp);
		}
		
		public function setSubject(sub:FeedBackView = null):void
		{
			_subject = sub;
		}
		
		public function get titleTips():String { return _titleTips.text; }
		public function get inputText():String { return _inputText.text; }
		public function get tabSelectIndex():int { return _tabBar.selectItem.index; }
		public function get upLoadSelect():Boolean { return _upLoad.select; }
	}

}